﻿using UnityEngine;
using System.Collections;
using LitJson;

public class FpsShow : MonoBehaviour
{
    //public UILabel m_FPS;
    float _updateInterval = 0.5f;//设定更新帧率的时间间隔为1秒  
    float _accum = .0f;//累积时间  
    int _frames = 0;//在_updateInterval时间内运行了多少帧  
    float _timeLeft;

    public string str = "";

    public static FpsShow Instance;

    void Awake()
    {
        Instance = this;
    }

    float fps;
    void Start()
    {
//         if (!m_FPS)
//         {
//             enabled = false;
//             return;
//         }
        _timeLeft = _updateInterval;
    }

    void OnGUI()
    {
        GUI.Label(new Rect(0, 0, 100, 50), System.String.Format("{0:F2}FPS", fps));
        //Debug.LogError("11111111111111");
    }

    // Update is called once per frame  
    void Update()
    {
        _timeLeft -= Time.deltaTime;
        //Time.timeScale可以控制Update 和LateUpdate 的执行速度,  
        //Time.deltaTime是以秒计算，完成最后一帧的时间  
        //相除即可得到相应的一帧所用的时间  
        _accum += Time.timeScale / Time.deltaTime;
        ++_frames;//帧数  

        if (_timeLeft <= 0)
        {
             fps = _accum / _frames;
            //Debug.Log(_accum + "__" + _frames);  
           // string fpsFormat = System.String.Format("{0:F2}FPS", fps);//保留两位小数  
           // m_FPS.text = fpsFormat;

            _timeLeft = _updateInterval;
            _accum = .0f;
            _frames = 0;
        }
        if (str != null && str != "")
        {
            Notification node = new Notification(str);
            JSONNode ad = new JSONNode();
            ad = JSON.Parse(str);
            switch (int.Parse(ad["cmd"].ToString()))
            {
                case (int)ClientPoll.pollEvent.Regist_Back_Event:
                    NotficationCenter.Instance.PostNotificationEvent(ClientPoll.pollEvent.Regist_Back_Event.ToString(), node);
                    break;
                case (int)ClientPoll.pollEvent.Login_Back_Event:
                    NotficationCenter.Instance.PostNotificationEvent(ClientPoll.pollEvent.Login_Back_Event.ToString(), node);
                    break;
                default:
                    break;
            }
            str = "";
        }

        
    }
}  
